import Vue from 'vue'
import Router from 'vue-router'
import Home from '../views/home'
import About from '../views/about'
import Center from '../views/center'
import Search from '../views/search'
import Wode from '@/views/wode'
import Order from '@/views/wode/order'
import Address from '@/views/wode/address'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(Router)

export const routes = [
    {
        path: '/',
        components: {
            default: Home,
            nav: Home,
        },
        title: '首页',
        isExact: true,
        meta: {
            isNav: true,
            til: '首页',
        },
    },
    {
        path: '/center/:id',
        component: Center,
        components: {
            default: Center,
            nav: Center,
        },
        meta: {
            til: '详情',
        },
    },
    {
        path: '/about',
        component: About,
        components: {
            default: About,
            nav: About,
        },
        meta: {
            til: '歌曲列表',
        },
    },
    {
        path: '/search',
        // component: Search,
        // meta: {
        //     isNav: true,
        // },
        meta: {
            til: '表单',
        },
        components: {
            default: Search,
            nav: Search,
        },
    },
    {
        path: '/wode',
        component: Wode,
        title: '我的',
        redirect: '/wode/order',
        meta: {
            isNav: true,
            til: '我的',
        },
        children: [
            {
                path: '/wode/order',
                component: Order,
                title: '订单',
                meta: {
                    isNav: true,
                    til: '我的订单',
                },
            },
            {
                path: '/wode/address',
                component: Address,
                title: '地址',
                meta: {
                    isNav: true,
                    til: '我的地址',
                },
            },
        ],
    },
]

const router = new Router({
    mode: 'history',
    routes,
})

router.beforeEach((to, from, next) => {
    console.log(to, 'to')
    console.log(from, 'from')
    document.title = to.meta.til
    NProgress.start() // 开始显示进度条
    next()
})

router.afterEach(() => {
    setTimeout(() => {
        NProgress.done() // 完成路由切换，隐藏进度条
    }, 1000)
})

export default router
